//
// Created by xdc on 2025/11/9.
//

#include "return.h"
void return_book() {
    const char *check_status_sql = "select user_status from front_user where id=?";
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(db, check_status_sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "归还失败：查询用户状态出错 - %s\n", sqlite3_errmsg(db));
        return;
    }
    sqlite3_bind_int(stmt, 1, login_user_id);
    int user_status = 1; // 默认视为拉黑
    if (sqlite3_step(stmt) == SQLITE_ROW) {
        user_status = sqlite3_column_int(stmt, 0);
    } else {
        fprintf(stderr, "归还失败：未找到当前用户信息\n");
        sqlite3_finalize(stmt);
        return;
    }
    sqlite3_finalize(stmt);
    if (user_status == 1) {
        printf("归还失败：您的账号已被拉黑，操作受限\n");
        return;
    }

    char book_name[50];
    printf("\n===== 图书归还 =====\n");
    printf("当前登录用户ID：%d\n", login_user_id);
    printf("请输入要归还的图书名字：");
    scanf("%s", book_name);

    const char *check_borrow_sql =
            "select record_id from borrow_record "
            "where user_id=? and book_name=? and status=0;";
    rc = sqlite3_prepare_v2(db, check_borrow_sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "查询借阅记录预编译失败：%s\n", sqlite3_errmsg(db));
        return;
    }
    sqlite3_bind_int(stmt, 1, login_user_id);
    sqlite3_bind_text(stmt, 2, book_name, -1, SQLITE_TRANSIENT);

    rc = sqlite3_step(stmt);
    if (rc != SQLITE_ROW) {
        printf("归还失败：未查询到您借阅《%s》的记录（或已归还）\n", book_name);
        sqlite3_finalize(stmt);
        return;
    }
    int borrow_id = sqlite3_column_int(stmt, 0);
    sqlite3_finalize(stmt);

    const char *update_record_sql =
            " update borrow_record "
            " set status=1,return_time=datetime('now', 'localtime') "
            " where record_id=?;"; // status=1表示已归还
    rc = sqlite3_prepare_v2(db, update_record_sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "更新归还状态预编译失败：%s\n", sqlite3_errmsg(db));
        return;
    }

    sqlite3_bind_int(stmt,1,borrow_id);
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        fprintf(stderr, "更新归还状态失败：%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return;
    }
    sqlite3_finalize(stmt);

    const char *update_stock_sql =
            "update book set stock=stock+1 where book_name=?";
    rc = sqlite3_prepare_v2(db, update_stock_sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "恢复库存预编译失败：%s\n", sqlite3_errmsg(db));
        return;
    }
    sqlite3_bind_text(stmt, 1, book_name, -1, SQLITE_TRANSIENT);
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        fprintf(stderr, "恢复库存失败：%s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        return;
    }
    sqlite3_finalize(stmt);

    printf("归还成功！《%s》已归还\n", book_name);

    const char *check_stock_sql = "select stock from book where book_name=?";
    rc = sqlite3_prepare_v2(db, check_stock_sql, -1, &stmt, NULL);
    if (rc == SQLITE_OK) {
        sqlite3_bind_text(stmt, 1, book_name, -1, SQLITE_TRANSIENT);
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            int new_stock = sqlite3_column_int(stmt, 0);
            printf("当前《%s》库存：%d本\n", book_name, new_stock);
        }
        sqlite3_finalize(stmt);
    }
}